import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";
import _find from "lodash/find";

var _dec, _class;

/**
 * 角色切换
 */
import React, { PureComponent } from 'react';
import { Menu, Spin } from 'antd';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Bind } from 'lodash-decorators';
import NProgress from "../../../components/NProgress";
import { getCurrentRole } from "../../../utils/utils";
import { cleanMenuTabs } from "../../../utils/menuTab";
import styles from "./index.less?modules";
var DefaultRoleSelect = (_dec = Bind(), (_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(DefaultRoleSelect, _PureComponent);

  var _super = _createSuper(DefaultRoleSelect);

  function DefaultRoleSelect() {
    _classCallCheck(this, DefaultRoleSelect);

    return _super.apply(this, arguments);
  }

  _createClass(DefaultRoleSelect, [{
    key: "onClick",
    value: function onClick(_ref) {
      var key = _ref.key;
      var _this$props = this.props,
          _this$props$user = _this$props.user,
          user = _this$props$user === void 0 ? {} : _this$props$user,
          updateCurrentRole = _this$props.updateCurrentRole,
          updateCurrentUser = _this$props.updateCurrentUser,
          gotoTab = _this$props.gotoTab;
      var _user$roleList = user.roleList,
          roleList = _user$roleList === void 0 ? [] : _user$roleList;
      var newDefaultRole = _find(roleList, function (o) {
        return String(o.id) === key;
      }) || {};
      NProgress.start();
      var id = newDefaultRole.id,
          name = newDefaultRole.name;
      updateCurrentRole({
        roleId: id
      }).then(function (res) {
        NProgress.done();

        if (!(res && res.failed)) {
          updateCurrentUser({
            currentRoleId: id,
            currentRoleName: name
          }); // 切换到 工作台
          // warn 清空 tabs 信息

          cleanMenuTabs();
          gotoTab({
            pathname: '/workplace'
          });
          window.location.reload();
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props$user2 = this.props.user,
          user = _this$props$user2 === void 0 ? {} : _this$props$user2;
      var _user$roleList2 = user.roleList,
          roleList = _user$roleList2 === void 0 ? [] : _user$roleList2;
      return /*#__PURE__*/React.createElement(Menu, {
        className: "default-role-select-dropdown",
        selectedKeys: ["".concat((getCurrentRole() || {}).id)],
        onClick: this.onClick
      }, _isEmpty(roleList) ? /*#__PURE__*/React.createElement(Menu.Item, {
        key: "loading-spin"
      }, /*#__PURE__*/React.createElement(Spin, null)) : /*#__PURE__*/React.createElement(Menu.SubMenu, {
        className: styles['select-role-wrap'],
        title: /*#__PURE__*/React.createElement("div", {
          className: styles['select-role']
        }, /*#__PURE__*/React.createElement("span", null, getCurrentRole().name || ''))
      }, roleList.map(function (n) {
        return /*#__PURE__*/React.createElement(Menu.Item, {
          key: n.id,
          className: styles['select-role-item']
        }, /*#__PURE__*/React.createElement("div", {
          className: styles['select-role-item-divider']
        }, n.name));
      })));
    }
  }]);

  return DefaultRoleSelect;
}(PureComponent), (_applyDecoratedDescriptor(_class.prototype, "onClick", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "onClick"), _class.prototype)), _class));
export default connect(function (_ref2) {
  var _ref2$user = _ref2.user,
      user = _ref2$user === void 0 ? {} : _ref2$user;
  return {
    user: user
  };
}, function (dispatch) {
  return {
    // 更新当前角色(调接口)
    updateCurrentRole: function updateCurrentRole(payload) {
      return dispatch({
        type: 'user/updateCurrentRole',
        payload: payload
      });
    },
    // 更新用户信息
    updateCurrentUser: function updateCurrentUser(payload) {
      return dispatch({
        type: 'user/updateCurrentUser',
        payload: payload
      });
    },
    // 跳转到页面
    gotoTab: function gotoTab(location, state) {
      return dispatch(routerRedux.push(location, state));
    }
  };
})(DefaultRoleSelect);